/**
 * 通过项目id获取localStorage
 * @param {string} projectId 项目id
 */
export function getStoredData(projectId) {
  if (!window.localStorage) {
    return {}
  }
  const storegeStr = localStorage.getItem(projectId)
  if (storegeStr) {
    try {
      const storegeObj = JSON.parse(storegeStr).root
      return storegeObj || {}
    } catch (error) {
      console.log(error)
    }
  } else {
    return {}
  }
}

/**
 * 深度冻结
 * @param {*} obj 需要冻结的对象
 */
export function deepFreeze(obj) {
  // 取回定义在obj上的属性名
  let propNames = Object.getOwnPropertyNames(obj)

  // 在冻结自身之前冻结属性
  propNames.forEach(function (name) {
    let prop = obj[name]

    // 如果prop是个对象，冻结它
    if (typeof prop == 'object' && prop !== null) deepFreeze(prop)
  })

  // 冻结自身(no-op if already frozen)
  return Object.freeze(obj)
}
/**
 * MutationObserver
 */
export class MuObserver {
  constructor(dom, callback, config = {}) {
    let targetNode
    if (dom instanceof HTMLElement) {
      targetNode = dom
    } else {
      targetNode = document.querySelector(dom)
    }
    let defaultConfig = { attributes: true }
    config = { ...defaultConfig, ...config }
    if (typeof callback !== 'function') {
      return Object.create(null)
    }
    const observer = new MutationObserver(callback)
    observer.observe(targetNode, config)
    return observer
  }
}

function matrix(appDom) {
  const transform = getComputedStyle(appDom).transform
  return (transform.match(/\d+(.\d+)?/g) || []).map((s) => {
    return parseFloat(s) && 1 / parseFloat(s)
  })
}

function findFull(node, width, height, scale = 1) {
  if (!(node instanceof HTMLElement)) return false
  const styles = getComputedStyle(node)
  let eleWidth = parseInt(styles.width)
  let eleHeight = parseInt(styles.height)
  let backgroundImage = styles.backgroundImage

  if (
    backgroundImage.includes('url') &&
    Math.abs(eleWidth / scale - width) <= 5 &&
    Math.abs(eleHeight / scale - height) <= 5
  ) {
    return node
  } else {
    return false
  }
}

function everyEle(nodes, width, height, scale) {
  if (nodes instanceof NodeList) {
    nodes = [...nodes]
  } else {
    nodes = [nodes]
  }
  let list = []
  for (let i = 0; i < nodes.length; i++) {
    if (nodes[i].nodeType == 1) {
      let transform = matrix(nodes[i])
      if (transform[0] === transform[3] && transform[0] !== undefined) {
        scale = scale !== 1 ? scale : transform[0]
      }
      let result = findFull(nodes[i], width, height, scale)
      if (result) {
        list.push(result)
      }
      if (nodes[i].childNodes.length) {
        list = list.concat(everyEle(nodes[i].childNodes, width, height, scale))
      }
    }
  }
  return list
}
// added by WUYAX @2022-01-04
export function getFullNodes(node, rootWidth, rootHeight) {
  if (!(node instanceof HTMLElement)) return []
  let width = 0,
    height = 0

  if (node instanceof HTMLElement) {
    const styles = getComputedStyle(node)
    width = parseInt(styles.width)
    height = parseInt(styles.height)
  }

  if (rootWidth === undefined || rootHeight === undefined) {
    rootWidth = width
    rootHeight = height
  }
  return everyEle(node, rootWidth, rootHeight, 1)
}
